package com.groups.action;

import java.util.Date;
import java.util.List;
import java.util.Random;

import com.groups.common.SPageBean;
import com.groups.entity.Good;
import com.groups.entity.Order;
import com.groups.entity.User;
import com.groups.service.GoodInfoService;

public class GoodInfoAction extends BaseAction{
	private GoodInfoService goodService;
	private List<Good> goods;
	private Object[]  goodInfo;
	private Good good;
	private int goodkind;
	private Order order = new Order();
	private boolean flag;
	
	public int getGoodkind() {
		return goodkind;
	}

	public void setGoodkind(int goodkind) {
		this.goodkind = goodkind;
	}

	public Good getGood() {
		return good;
	}

	public void setGood(Good good) {
		this.good = good;
	}

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public Object[] getGoodInfo() {
		return goodInfo;
	}

	public void setGoodInfo(Object[] goodInfo) {
		this.goodInfo = goodInfo;
	}

	public List<Good> getGoods() {
		return goods;
	}

	public void setGoods(List<Good> goods) {
		this.goods = goods;
	}

	public GoodInfoService getGoodService() {
		return goodService;
	}

	public void setGoodService(GoodInfoService goodService) {
		this.goodService = goodService;
	}
	public String getNewGoodInfo(){
	  //  goods = goodService.getUpdateInfo();
		return "newGood";
	}
	public String toList() throws Exception{
		int offset = 0;
		if(request.getParameter("pager.offset")==null){
			offset = 0;
		}
		else{
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		}
		SPageBean spb = new SPageBean();
		System.out.println(offset);
		spb.setOffset(offset);
		spb.setPageSize(4);
		goods = goodService.getGoodsByPage(spb,goodkind);
		request.setAttribute("SPB", spb);
		return "newGood";
	}
	public String toBuyingProgress() throws Exception{
		return "progress";
	}
	public String toDetail(){
		return "detail";
	}
	public String getGoodDetail(){
		int goodId = Integer.parseInt(request.getParameter("goodId"));
		goodInfo = goodService.getGoodDetail(goodId);
		Date now = new Date();
		Date endTime = (Date) goodInfo[6];
		if(endTime.after(now)){
			flag = false;
		}else{
			flag = true;
		}
		request.setAttribute("GoodInfo", goodInfo);
		return "detail";
	}
	public String addOrder(){
		Good good = new Good();
		int goodId = (Integer) goodInfo[11];
		good = goodService.getGoodById(goodId);
		User user = (User) session.get("USER");
		String counts = request.getParameter("count");
		float price = (Float)goodInfo[1];
		int count = Integer.parseInt(counts);
		int orderId = goodService.madeOrderId();
		Date orderTime = new Date();
		good.setGoodId(goodId);
		float sumPrice = (float) (count*price);
		String username = user.getUserName();
		order.setOrderId(orderId);
		order.setCount(count);
		//order.getGood().setGoodId(goodId);
		order.setGood(good);
		order.setSumPrice(sumPrice);
		order.setUser(user);
		goodService.addOrder(order);
		return null;
	}
}
